package com.iflytek.application.common.springdata.jdbc.repository.utils;


import org.springframework.jdbc.datasource.DataSourceUtils;

import javax.sql.DataSource;
import java.sql.*;


public class DBSourceUtils {
    /**
     * 获取jdbc
     *
     * @return
     * @throws java.sql.SQLException
     */
    /*@Deprecated
	public static Connection getConnection() {
		Connection conn = null;
		String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=bms-data";
		String username = "sa";
		String password = "123456";
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, username, password);
			return conn;
		} catch (Exception e) {
			throw new RuntimeException("DBConnection Exception" + e.getMessage());
		}
	}*/
    public static Connection getConnection(DataSource dataSource) {
        return DataSourceUtils.getConnection(dataSource);
    }


    /**
     * <p>
     * 关闭Connection连接,ResultSet,Statement
     * </p>
     */
    public static void closeConnection(Connection conn, Statement stmt, ResultSet rs) {
        /*
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println("连接关闭失败。可能连接未创建成功！" + e.getMessage());
            }
        } else {
            System.out.println("=> [Connection未创建，无法关闭]");
        }
*/
    }


    /**
     * 关闭ResultSet、Statement和Connection
     *
     * @param rs   ResultSet to be closed
     * @param stmt Statement or PreparedStatement to be closed
     * @param conn Connection to be closed
     */
    public static void close(ResultSet rs, Statement stmt, Connection conn) {
       /* if (rs != null)
            try {
                rs.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }*/
    }

    public static void close(Statement stmt, Connection conn) {
      /*  if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }*/
    }

    public static void close(PreparedStatement pstmt, Connection conn) {
       /* try {
            if (pstmt != null)
                pstmt.close();
        } catch (Exception e) {
            // logger.error(e);
        }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }*/
    }

    public static void close(ResultSet rs, PreparedStatement pstmt) {
       /* try {
            if (pstmt != null)
                pstmt.close();
        } catch (Exception e) {
        }*/

    }

    public static void close(Connection conn) {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
    }
    public static void close(PreparedStatement pstmt) {
        try {
            if (pstmt != null)
                pstmt.close();
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

    public static void close(ResultSet rs, Statement stmt) {
        if (rs != null)
            try {
                rs.close();
            } catch (java.sql.SQLException ex) {
                ex.printStackTrace();
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (java.sql.SQLException ex) {
                ex.printStackTrace();
            }

    }

}
